/**
 * 
 */
package hihocoder;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * @author zhongfang
 *
 */
public class 顺序三元组 {

	public static void getRes(int A[]) {
		int n=A.length;
		int count3=0;
		long sum=0;
		ArrayList<Integer> last3count=new ArrayList<>();
		for (int i = n-1; i>=0; i--) {
			if(A[i]==3){
				count3++;
			}else if(A[i]==2){
				last3count.add(count3);//3的個數
			}else if (A[i]==1) {
				for (Integer integer : last3count) {
					sum+=integer;
				}
			}
		}
		System.out.println(sum);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		Scanner scanner=new Scanner(System.in);
//		int n=scanner.nextInt();
//		int[] A=new int[n];
//		for (int i = 0; i < n; i++) {
//			A[i]=scanner.nextInt();
//		}
		for (int j = 0; j < 10; j++) {
			int A[]=new int[100000];
			Random random=new Random();
			for (int i = 0; i < A.length; i++) {
				A[i]=1+random.nextInt(3);
			}
			getRes(A);
		}
	}
}
